/*
 * Created on 28.09.2005
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package de.fmannan.addbook.editor.actions;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.part.EditorActionBarContributor;

import de.fmannan.addbook.MyImageRegistry;
import de.fmannan.addbook.MyMessageRegistry;
import de.fmannan.addbook.editor.GenericEditor;

/**
 * @author fmannan
 *
 * Contributes Actions to the context menu of the {@link GenericEditor} which
 * appear as soon as the editor is opened.
 */
public class StandardEditorActionBarContributor extends EditorActionBarContributor {

    /**
     * Action to be triggered by the button.
     */
    private EditorAction saveAction;
    
    /**
     * Create the button contributor.
     */
    public StandardEditorActionBarContributor() {
        super();

        saveAction = new EditorAction (
                MyMessageRegistry.SAVE_EDITOR_TOOLTIP,
				MyImageRegistry.getImageRegistry().getDescriptor(
						MyImageRegistry.SAVE_ICON)) { 
            public void run() {
                getActiveEditor().doSave(new NullProgressMonitor());
            }
        };
    }

    public void contributeToToolBar(IToolBarManager toolBarManager) {
        super.contributeToToolBar(toolBarManager);
        
        toolBarManager.add(new Separator("Editor actions")); //$NON-NLS-1$
        toolBarManager.add(saveAction);
    }
    
    public void setActiveEditor(IEditorPart editor) {
        super.setActiveEditor(editor);
        // Target shared actions with new editor
        saveAction.setActiveEditor(editor);
    }

    /**
	 * @author fmannan
	 * 
	 * Save action object.
	 */
    private static class EditorAction extends Action {

        /**
         * Memorized editor to save entity in.
         */
        private IEditorPart activeEditor;
        
        /**
         * Create action.
         * 
         * @param text Text to display.
         * @param image Image to display.
         */
        public EditorAction(String text, ImageDescriptor image) {
            super(text, image);
        }
        
        
        /**
         *  Set the memorized editor.
         */
        void setActiveEditor(IEditorPart editor) {
            activeEditor = editor;
        }
        
        /**
         *  Get the memorized editor.
         */
        protected IEditorPart getActiveEditor() {
            return activeEditor;
        }
    }
        
}
